4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
11 // You must not remove this notice, or any other, from this software.
16 namespace Microsoft
.JScript
{
18 using System
.Threading
;
19 using System
.Reflection
;
20 using System
.Reflection
.Emit
;
21 using System
.Security
;
22 using System
.Security
.Permissions
;
23 using Microsoft
.JScript
.Vsa
;
25 public static class Runtime
{
26 // *** VS58565 - JSEE: Remove all uses of IDebugObject::GetManagedObject from the code ***
27 // JSEE does a funceval to this method to compare if to value types are equal.
28 public static new bool Equals(Object v1
, Object v2
){
29 Equality e
= new Equality((int)JSToken
.Equal
);
30 return e
.EvaluateEquality(v1
, v2
);
35 public static Int64
DoubleToInt64(double val
) {
36 if (Double
.IsNaN(val
)) { // Take care of NaN case first as it isn't ordered.
39 if (Int64
.MinValue
<= val
&& val
<= Int64
.MaxValue
) {
43 // Remove cases where IEEERemainder will return NaN.
44 if (Double
.IsInfinity(val
))
47 // Definition of IEEERemandier returns:
48 // A number equal to x-(y*Q), where Q is the quotient of x/y rounded to the nearest
49 // integer (if x/y falls halfway between two integers, the even integer is returned).
51 // We use y = 2^64 = UInt64.MaxValue+1. The resulting remainder may range from -2^63
52 // to 2^63. i.e. x = (2^64+2^63) gives Q = 2 and result -2^63, x = -(2^64+2^63) gives
53 // Q = -2 and result 2^63. To fit into Int64, we translate 2^63 by -2^64 to get -2^63
54 // which is Int64.MinValue.
55 double rem
= Math
.IEEERemainder(Math
.Sign(val
) * Math
.Floor(Math
.Abs(val
)), (double)UInt64
.MaxValue
+1.0);
56 if (rem
== (double)Int64
.MaxValue
+1.0) {
57 return Int64
.MinValue
;
63 private const Decimal DecimalTwoToThe64
= (Decimal
)UInt64
.MaxValue
+1;
65 public static Int64
UncheckedDecimalToInt64(Decimal val
) {
67 val
= Decimal
.Truncate(val
);
69 // Ensure value is in range.
70 if (val
< Int64
.MinValue
|| Int64
.MaxValue
< val
) {
71 val
= Decimal
.Remainder(val
, DecimalTwoToThe64
);
72 if (val
< Int64
.MinValue
)
73 val
+= DecimalTwoToThe64
;
74 else if (val
> Int64
.MaxValue
)
75 val
-= DecimalTwoToThe64
;
78 // Checked coercion is guaranteed to succeed
79 Debug
.Assert(Int64
.MinValue
<= val
&& val
<= Int64
.MaxValue
);
84 // RUNTIME TYPEREFERENCES
85 private static TypeReferences _typeRefs
;
86 internal static TypeReferences TypeRefs
{
88 TypeReferences typeRefs
= Runtime
._typeRefs
;
90 typeRefs
= Runtime
._typeRefs
= new TypeReferences(typeof(Runtime
).Module
);
95 // ASSEMBLY AND MODULE BUILDER FOR FIELDACCESSOR AND METHODINVOKER THUNKS
96 private static ModuleBuilder _thunkModuleBuilder
;
97 internal static ModuleBuilder ThunkModuleBuilder
{
99 ModuleBuilder moduleBuilder
= Runtime
._thunkModuleBuilder
;
100 if (null == moduleBuilder
)
101 moduleBuilder
= Runtime
._thunkModuleBuilder
= CreateThunkModuleBuilder();
102 return moduleBuilder
;
106 [ReflectionPermission(SecurityAction
.Assert
, ReflectionEmit
= true), FileIOPermission(SecurityAction
.Assert
, Unrestricted
= true)]
107 private static ModuleBuilder
CreateThunkModuleBuilder() {
108 AssemblyName name
= new AssemblyName();
109 name
.Name
= "JScript Thunk Assembly";
110 AssemblyBuilder assembly
= Thread
.GetDomain().DefineDynamicAssembly(name
, AssemblyBuilderAccess
.Run
);
111 ModuleBuilder module
= assembly
.DefineDynamicModule("JScript Thunk Module");
112 module
.SetCustomAttribute(new CustomAttributeBuilder(typeof(SecurityTransparentAttribute
).GetConstructor(new Type
[0]), new Object
[0]));